﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Data;
using WPFExcelReport.Helpers;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using INTEX.SCHEDULER;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    /// 
    public partial class ITXAPS
    {
        private void btnRefreshMachine_Click(object sender, RoutedEventArgs e)
        {
            DataRow dwData = SetEnv.ICC_MKRF.NewRow();
            DataSet SResult = INTEXDBC.QueryICC_MKRF(dwData, 4);
            SResult.Tables[0].TableName = "ICC_MKRF";
            if (SetProd.Tables.Contains("ICC_MKRF"))
            {
                SetProd.Tables["ICC_MKRF"].Clear();
            }
            SetProd.Merge(SResult);
            Scheduler.PrepareAllJob(SetProd,false);
        }

        private void btnSaveEvents_Click(object sender, RoutedEventArgs e)
        {
            if (dwMRILI == null)
            {
                return;
            }
            var qM = from DataRow row in SetProd.Tables["ICC_MKRF"].AsEnumerable()
                     where row["JOBTYPE"].ToString() == "X"
                     select row;
            foreach (DataRow row in qM)
            { 
                INTEXDBC.SaveICC_MKRF(dwMRILI);
            }
        }

        private void btnAddEvents_Click(object sender, RoutedEventArgs e)
        {
            if (dwAMSTAMM == null)
            {
                MessageBox.Show("请选择设备");
                return;
            }
            try
            {
                string date = txtMDate.Text;
                if (date.Trim().Length != 8)
                {
                    MessageBox.Show("请设定正确的日期");
                    return;
                }
                string filter = "ABFAUFKRS='RL' AND ABFAUFLFD='" + date.Substring(0, 6) + "' AND ABFAUFPOS='" + Helpers.Helper.Right("000" + date.Substring(6, 2), 3) + "' AND MKFTP='0000'";
                DataRow[] rows = SetProd.Tables["ICC_MKRF"].Select(filter);
                DataRow dwNMRKF = null;
                DataRow dwXMRKF = null;
                if (rows.Length == 0)
                {
                    dwXMRKF = SetProd.Tables["ICC_MKRF"].Rows.Add();
                    //dwXMRKF.ItemArray = INTEXDBC.Convert2ICC_MKRF(null, "YGR", false).ItemArray;
                    dwXMRKF["ABAID"] = INTEXDBC.GetSeq();
                    dwXMRKF["ABFAUFKRS"] = "RL";
                    dwXMRKF["ABFAUFLFD"] = date.Substring(0, 6);
                    dwXMRKF["ABFBNRKRS"] = dwAMSTAMM["AMNRKRS"];
                    dwXMRKF["ABFBLFDNR"] = dwAMSTAMM["AMLFDNR"];
                    dwXMRKF["ABFBPOS"] = dwAMSTAMM["AMPOS"];
                    dwXMRKF["MACHINEID"] = dwAMSTAMM["MACHINEID"];
                    dwXMRKF["ABFAUFPOS"] = Helpers.Helper.Right("000" + date.Substring(6, 2), 3);
                    dwXMRKF["ABPART"] = Helper.ConvertGB2312To8859P1("请定义");
                    dwXMRKF["MKFTP"] = "0000";
                    dwXMRKF["MKFPLDATV"] = date;
                    dwXMRKF["MKFPLTIME"] = 0;
                    dwXMRKF["ABFAMASCHZT"] = 0;
                    dwXMRKF["JP50"] = "0";
                    dwXMRKF["ABFFIX"] = "J";
                    dwXMRKF["JOBTYPE"] = "X";
                    dwXMRKF["ABFISTARTDAT"] = "0";
                    dwXMRKF["CR"] = "108";
                    dwXMRKF["CG"] = "108";
                    dwXMRKF["CB"] = "108";
                }
                else
                {
                    dwXMRKF = rows[0];
                }
                int NNUM = Convert.ToInt16(dwXMRKF["JP50"].ToString());
                NNUM = NNUM + 1;
                dwXMRKF["JP50"] = NNUM;
                INTEXDBC.SaveICC_MKRF(dwXMRKF);

                dwNMRKF = SetProd.Tables["ICC_MKRF"].Rows.Add();
                dwXMRKF.ItemArray = dwXMRKF.ItemArray;// INTEXDBC.Convert2ICC_MKRF(dwXMRKF, "YGR", true).ItemArray;
                dwNMRKF["ABAID"] = INTEXDBC.GetSeq();
                dwNMRKF["ABFBNRKRS"] = dwAMSTAMM["AMNRKRS"];
                dwNMRKF["ABFBLFDNR"] = dwAMSTAMM["AMLFDNR"];
                dwNMRKF["ABFBPOS"] = dwAMSTAMM["AMPOS"];
                dwNMRKF["MACHINEID"] = dwAMSTAMM["MACHINEID"];
                dwNMRKF["ABFMANDANT"] = dwAMSTAMM["AMMANDANT"];
                dwNMRKF["ABFKSTKRS"] = dwAMSTAMM["AMKSTKRS"];
                dwNMRKF["ABFKST"] = dwAMSTAMM["AMKST"];

                dwNMRKF["MKFTP"] = Helper.Right("0000" + NNUM, 4);
                dwNMRKF["ABPART"] = Helper.ConvertGB2312To8859P1("请定义");

                #region=没有当日的维修记录===

                #endregion
                Scheduler.PrepareAllJob(SetProd, false);
                if (vwMRILI.View != null)
                {
                    vwMRILI.View.MoveCurrentToLast();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRLToAll_Click(object sender, RoutedEventArgs e)
        {
            if (dwMRILI == null)
            {
                MessageBox.Show("请选择事件");
                return;
            }
            try
            {
                string date = txtMDate.Text;
                if (date.Trim().Length != 8)
                {
                    MessageBox.Show("请设定正确的日期");
                    return;
                }
                string filter = "ABFAUFKRS='RL' AND ABFAUFLFD='" + dwMRILI["ABFAUFLFD"].ToString() + "' AND ABFAUFPOS='" + dwMRILI["ABFAUFPOS"].ToString() + "' AND MKFTP='0000'";
                DataRow[] rows = SetProd.Tables["ICC_MKRF"].Select(filter);
                DataRow dwNMRKF = null;
                DataRow dwXMRKF = null;
                int NNUM = 0;
                string MachineID = "";

                dwXMRKF = rows[0];
                NNUM = Convert.ToInt16(dwXMRKF["JP50"].ToString());
                MachineID = dwXMRKF["MACHINEID"].ToString();
                var qM = from DataRow row in SetEnv.Tables["AMSTAMM"].AsEnumerable()
 where row["MACHINEID"].ToString() != MachineID
 select row;
                foreach (DataRow dwMachine in qM)
                {

                    NNUM = NNUM + 1;
                    dwNMRKF = SetProd.Tables["ICC_MKRF"].Rows.Add();
                    dwNMRKF.ItemArray = dwXMRKF.ItemArray;// INTEXDBC.Convert2ICC_MKRF(dwXMRKF, "YGR", true).ItemArray;
                    dwNMRKF["ABAID"] = INTEXDBC.GetSeq();
                    dwNMRKF["ABFBNRKRS"] = dwMachine["AMNRKRS"];
                    dwNMRKF["ABFBLFDNR"] = dwMachine["AMLFDNR"];
                    dwNMRKF["ABFBPOS"] = dwMachine["AMPOS"];
                    dwNMRKF["MACHINEID"] = dwMachine["MACHINEID"];
                    dwNMRKF["MKFTP"] = Helper.Right("0000" + NNUM, 4);
                    dwNMRKF["ABPART"] = dwXMRKF["ABPART"];
                    dwNMRKF["MKFPLDATV"] = dwXMRKF["MKFPLDATV"];
                    dwNMRKF["MKFPLTIME"] = dwXMRKF["MKFPLTIME"];
                    dwNMRKF["ABFAMASCHZT"] = dwXMRKF["ABFAMASCHZT"];
                    INTEXDBC.SaveICC_MKRF(dwNMRKF);
                }

                dwXMRKF["JP50"] = NNUM;
                INTEXDBC.SaveICC_MKRF(dwXMRKF);
                Scheduler.PrepareAllJob(SetProd, false);
                if (vwMRILI.View != null)
                {
                    vwMRILI.View.MoveCurrentToLast();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        private void btnEditJob_Click(object sender, RoutedEventArgs e)
        {
            if (dwBMKRF == null)
            {
                MessageBox.Show("请选择任务");
                return;
            }
            string JobID = "Job" + dwBMKRF["ABAID"].ToString();
            JobView job = (JobView)JobsLib[JobID];
            JobDetailWindow JobDetailWindow = new JobDetailWindow();
            JobDetailWindow.DataContext = job.DataContext;
            JobDetailWindow.Show();
        }
        void vwMRILI_CurrentChanged(object sender, EventArgs e)
        {
            if (dwMRILI == null)
            {
                return;
            }
            string JobID = "Job" + dwMRILI["ABAID"].ToString();
            JobView job = (JobView)JobsLib[JobID];
            job.dwJob = dwMRILI;
            EditMachineJob.DataContext = job.DataContext;
        }
        void vwAMSTAMM_CurrentChanged(object sender, EventArgs e)
        {
            if (dwAMSTAMM == null)
            {
                return;
            }
            string filter = "MACHINEID='" + dwAMSTAMM["MACHINEID"].ToString()+"'";
            cvMRili.CustomFilter = filter;
        }
        private void btnMClear_Click(object sender, RoutedEventArgs e)
        {
            cvMRili.CustomFilter = null;
        }

        private void btnMFilter_Click(object sender, RoutedEventArgs e)
        {
            if (txtDate.Text.ToString().Trim() == "")
            {
                MessageBox.Show("请输入截止日期!");
                return;
            }
            string filter = "MACHINEID" + dwAMSTAMM["MACHINEID"].ToString();
            cvMRili.CustomFilter = filter;
        }
    }
}
